﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharp7;

namespace PLC_S7200
{
    public class PLC_S7200
    {
        S7Client _PlcConnector = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool Init(object args)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Port))
                {
                    _PlcConnector = new S7Client();
                    _PlcConnector.ConnectTo(this.Port, 0, 1);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool Close(object args)
        {
            if (_PlcConnector == null) return true;
            _PlcConnector.Disconnect();
            return true;
        }

        object locker = new object();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="regType"></param>
        /// <param name="regAddr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public ushort[] ReadRegister(byte regType, string regAddr, int length)
        {
            lock (locker)
            {
                int startaddr = int.Parse(regAddr);
                byte[] buffer = new byte[length * 2];
               int res= _PlcConnector.ReadArea(S7Consts.S7AreaDB, 1, startaddr, length*2, S7Consts.S7WLByte, buffer);
               if (res != 0)
               {
                   IOHelper.DebugMsg.Log("PLC S200  SetRegister Error ..." + res.ToString());
               }
                ushort[] resbuffer = new ushort[length];
                for (int i = 0; i < length; i++)
                {

                    resbuffer[i] = S7.GetUIntAt(buffer, i * 2);
                }
                return resbuffer;
            }
             //_PlcConnector.re
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="regAddr"></param>
        /// <returns></returns>
        public byte ReadIO(string regAddr)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="regType"></param>
        /// <param name="regAddr"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool SetRegister(byte regType, string regAddr, ushort val)
        {
            lock (locker)
            {
                int startaddr = int.Parse(regAddr);
                byte[] buffer = new byte[ 2];
                S7.SetUIntAt(buffer, 0, val);
                int rs=_PlcConnector.DBWrite( 1, startaddr, 2, buffer);
                if (rs != 0)
                {
                    IOHelper.DebugMsg.Log("PLC S200  SetRegister Error ..."+rs.ToString());
                }
                return true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="regType"></param>
        /// <param name="startRegAddr"></param>
        /// <param name="length"></param>
        /// <param name="vals"></param>
        /// <returns></returns>
        public bool SetRegisters(string regType, string startRegAddr, int length, ushort[] vals)
        {
            throw new NotImplementedException();
        }
        string _port = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string Port
        {
            get
            {
                return _port;
            }
            set
            {
                _port=value;
            }
        
        }
        string _devName = string.Empty;

        /// <summary>
        /// 
        /// </summary>
        public string DevName
        {
            get
            {
                return _devName;
            }
            set
            {
                _devName=value;
            }
        }


        public float[] ReadRegisterF(byte regType, string regAddr, int length)
        {
         
            lock (locker)
            {
                int startaddr = int.Parse(regAddr);
                byte[] buffer = new byte[length * 4];
               int res= _PlcConnector.ReadArea(S7Consts.S7AreaDB, 1, startaddr, length*4, S7Consts.S7WLByte, buffer);
               if (res != 0)
               {
                   IOHelper.DebugMsg.Log("PLC S200  ReadRegister Error ..." + res.ToString());
               }
                float[] resbuffer = new float[length/2];
                for (int i = 0; i < resbuffer.Length; i++)
                {

                    resbuffer[i] = S7.GetRealAt(buffer, i*4);
                }
                return resbuffer;
            }
        }

        public ushort[] ReadRegisterDB(byte regType, string regAddr, int length, byte devID = 1)
        {
            throw new NotImplementedException();
        }
    }
}
